<?php

namespace App\Services;

use App\Models\AdminEstate;
use App\Models\EstateMenu;
use App\Models\Property;
use App\Models\PropertyCheck;
use App\Models\SmsLog;
use App\Models\User;
use App\Traits\HelperTrait;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class EstateService extends BaseService
{

    /**
     * 物业注册
     * @param string $username
     * @param string $auth
     * @return array
     */
    public function register($username = 'company_name', $auth = 'user'): array
    {

        //企业名称 company_name
        //法人代表 legal_person
        //企业详细地址 address
        //注册资本 registered_capital
        //成立日期 date_of_establishment
        //联系人 emergency_contact
        //联系电话 emergency_contact_phone
        // //统一信用代码 business_license_no
        //营业执照 business_license
        //有效期 valid_period
        //一般纳税人证明 general_taxpayer_certificate

        //物业信息主表需要字段
        $arr = [
            'username' => request()->username,
            'password' => request()->password,
            're_password' => request()->re_password,
            'code' => request()->code
        ];
        $arr1 = [];
        //物业信息审核表需要的字段
        $arr1 = [
                    'company_name' => request()->company_name,   //企业名称
                    'legal_person' => request()->legal_person,   //法人代表
                    'address' => request()->address,             //企业详细地址
                    'unified_social_credit_code' => request()->unified_social_credit_code,  //(统一信用代码)
                    'registered_capital' => request()->registered_capital,  //注册资本
                    'date_of_establishment' => request()->date_of_establishment,  //成立时间
                    'contact_person' => request()->contact_person,  //联系人
                    'contact_person_mobile' => request()->contact_person_mobile,  //联系人电话
                    'business_license_no' => request()->business_license_no,  //营业执照号
                    'electronic_version_business_license' => request()->electronic_version_business_license,  //营业执照图片
                    'validity_period_business_license' => request()->validity_period_business_license,  //营业执照有效期
                    'tax_certificate' => request()->tax_certificate,  //一般纳税人证明，
                    'email' => request()->email,
                    'check' => 0,

                ];

        $validator = Validator::make($arr1, [
            'company_name' => 'required',
            'legal_person' => 'required',
            'address' => 'required',
            'registered_capital' => 'required',
            'date_of_establishment' => 'required',
            'contact_person' => 'required',
            'contact_person_mobile' => 'required',
            'business_license_no' => 'required',
            'tax_certificate' => 'required',

        ], [
            'company_name.required' => '请填写企业名称',
            'legal_person.required' => '请填写法人',
            'address.required' => '请填写企业地址',
            'registered_capital.required' => '请填写注册资本',
            'date_of_establishment.required' => '请填写成立日期',
            'contact_person.required' => '请填写联系人',
            'contact_person_mobile.required' => '请填写联系人电话',
            'business_license_no.required' => '请填写营业执照号',
            'electronic_version_business_license.required' => '请上传营业执照图片',
            'tax_certificate.required' => '请上传一般人纳税证明',

        ]);

        if ($validator->fails()) {
            return $this->format_error(($validator->errors()->first()));
        }

        //查看物业名称数据是否已经存在
        $user_model = new Property();
        if (  $user_model->where('name', $arr1['company_name'])->exists()) {
            return $this->format_error(__('auth.user_exists'));
        }

        //数据存入物业信息主表
        $user_model->name = $arr1['company_name'];   //公司名称
        $user_model->account = $arr[$username];     //手机号存入账号字段
//        dd(Hash::make($arr['password']),$arr['password']);
        $user_model->password = Hash::make($arr['password']);
        $user_model->address = $arr1['address'];
        $user_model->principal = $arr1['contact_person']; //联系人
        $user_model->principal_mobile = $arr1['contact_person_mobile']; //联系电话
        $El = $user_model->save();
        if ($El)
        {
            $arr1['property_id']=$user_model->id;
            //创建物业信息相关表
            PropertyCheck::insert($arr1);
        }
        $data = [
            'success' => 1,
        ];
        return $this->format($data, '申请成功请耐心等待审核结果');
    }


}
